package com.honghu.villagepost.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

import com.honghu.villagepost.R;
import com.blankj.utilcode.util.SizeUtils;


/**
 * @author bsnl_yanxuwen
 * @date 2020/2/14 14:53
 * Description :
 * 部分圆角
 * 使用glide时候，不能使用transition
 */
public class RoundImageView extends AppCompatImageView {
    //最后确认的宽高
    private RectF drawRectF;
    private Paint mPaint;
    private Matrix matrix;
    private BitmapShader bitmapShader;
    private boolean topLeftCorner;
    private boolean topRightCorner;
    private boolean bottomLeftCorner;
    private boolean bottomRightCorner;
    private float radius = 5;

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        topLeftCorner = typedArray.getBoolean(R.styleable.RoundImageView_topLeftCorner, false);
        topRightCorner = typedArray.getBoolean(R.styleable.RoundImageView_topRightCorner, false);
        bottomLeftCorner = typedArray.getBoolean(R.styleable.RoundImageView_bottomLeftCorner, false);
        bottomRightCorner = typedArray.getBoolean(R.styleable.RoundImageView_bottomRightCorner, false);
        radius = typedArray.getDimension(R.styleable.RoundImageView_roundImageRadius, SizeUtils.dp2px(5));
        typedArray.recycle();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        matrix = new Matrix();
    }

    public float getRadius() {
        return radius;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        drawRectF = new RectF(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //创建一个和原图大小的图片
        if (this.getDrawable() == null) {
            return;
        }
        Bitmap sourceBitMap = null;
        try {
            sourceBitMap = ((BitmapDrawable) this.getDrawable()).getBitmap();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (sourceBitMap == null) {
            return;
        }
        //创建一个和原图大小的图片
        long startTime = System.currentTimeMillis(); //起始时间
        sourceBitMap = ((BitmapDrawable) this.getDrawable()).getBitmap();
        long endTime = System.currentTimeMillis(); //结束时间
        long runTime = endTime - startTime;
//        Log.i("test", String.format("方法使用时间 %d ms", runTime));

        //BitmapShader 为着色器
        bitmapShader = new BitmapShader(sourceBitMap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        // float scaleMax = 1.0f,scaleX=1.0f,scaleY=1.0f;
        float scaleMax = 1.0f;
        int dwidth = sourceBitMap.getWidth();
        int vheight = getMeasuredHeight();
        int vwidth = getMeasuredWidth();
        int dheight = sourceBitMap.getHeight();

        float dx = 0, dy = 0;
        if (dwidth * vheight > vwidth * dheight) {
            scaleMax = (float) vheight / (float) dheight;
            dx = (vwidth - dwidth * scaleMax) * 0.5f;
        } else {
            scaleMax = (float) vwidth / (float) dwidth;
            dy = (vheight - dheight * scaleMax) * 0.5f;
        }

        //对我们创建出来的bitmap进行缩放
        matrix.setScale(scaleMax, scaleMax);
        matrix.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
        bitmapShader.setLocalMatrix(matrix);
        mPaint.setShader(bitmapShader);
        //纠正圆角
//        int radius = (int) (getRadius() * scaleMax);
        //画出我们需要的直角图形
        canvas.drawRoundRect(drawRectF, radius, radius, mPaint);

        if (!topLeftCorner) {
            //左边顶部
            canvas.drawRect(0, 0, radius, radius, mPaint);
        }
        if (!topRightCorner) {
            //右边顶部
            canvas.drawRect(getWidth() - radius, 0, getWidth(), radius, mPaint);
        }
        if (!bottomLeftCorner) {
            //左边底部
            canvas.drawRect(0, getHeight() - radius, radius, getHeight(), mPaint);
        }
        if (!bottomRightCorner) {
            //右边底部
            canvas.drawRect(getWidth() - radius, getHeight() - radius, getWidth(), getHeight(), mPaint);
        }

    }
}